package com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.impl;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;

/**
 * User: jason
 * Date: 2013-02-20
 */
public abstract class BadCharacterUtils {
    private static final String REGEX_CUSTOMER_NAME = "[^A-Za-z\\s]";
    private static final int MAX_GUEST_NAME_LENGTH = 64;

    public static String filterName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        String filtered = name
                .replaceAll("1", "ONE ")
                .replaceAll("2", "TWO ")
                .replaceAll("3", "THREE ")
                .replaceAll("4", "FOUR ")
                .replaceAll("5", "FIVE ")
                .replaceAll("6", "SIX ")
                .replaceAll("7", "SEVEN ")
                .replaceAll("8", "EIGHT ")
                .replaceAll("9", "NINE ")
                .replaceAll("0", "ZERO ");
        return reserveChars(filterByRegex(filtered.toUpperCase(), REGEX_CUSTOMER_NAME), MAX_GUEST_NAME_LENGTH);
    }

    public static String filterByRegex(String str, String regex) {
        return filterByRegex(str, regex, "");
    }

    public static String filterByRegex(String str, String regex, String replacer) {
        Preconditions.checkArgument(StringUtils.isNotBlank(regex),
                "Regex is required!");
        if (StringUtils.isBlank(str)) {
            return null;
        }
        return str
                .replaceAll(regex, replacer)
                .trim()
                .replaceAll("\\s\\s+", " ");
    }

    public static String reserveChars(String src, int reserveChars) {
        if (StringUtils.isBlank(src)) {
            return src;
        }
        if (src.length() <= reserveChars) {
            return src;
        }
        return src.substring(0, reserveChars);
    }
}
